﻿/******************************************************************
** 文 件 名: Bu_AuditTable_FGW_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2013/3/1 17:19:35
** 描    述: 资金支付汇总表-发改委返回编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using System.Collections.Generic;


namespace Honestysoft.Financing.WebUI.Module.Audit
{
	public partial class Bu_AuditTable_FGW_Edit : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                AuditSummaryUtils.ThisTimeFGWSession = null;//每次都先清空Session 
				BindControl();
                Bind();
            }
        }


		private void BindControl()
		{
            DataTable dt = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetFGWBatch();
            ddlBatchNo.DataSource = dt;
            ddlBatchNo.DataTextField = "BatchNo";
            ddlBatchNo.DataValueField = "ID";
            ddlBatchNo.DataBind();            
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                ddlBatchNo.Enabled = false;
            }
            else
            {
                if (ddlBatchNo.Items.Count == 0)
                {
                    ddlBatchNo.Items.Insert(0, new ListItem("没有可选择的批次", "0"));
                }
                else if (ddlBatchNo.Items.Count == 1)
                {
                    ddlBatchNo.SelectedIndex = 0;
                    ddlBatchNo.Enabled = false;
                }
                else
                {
                    ddlBatchNo.Items.Insert(0, new ListItem("请选择所需的批次", "0"));
                    ddlBatchNo.SelectedIndex = 0;
                }
                dtpPrintDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
            }
		}

        //private void Bind()
        //{
        //    Bu_AuditTable_FGW obj = null;
        //    if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
        //    {
        //        obj = new BaseBusinessLogic<Bu_AuditTable_FGW>().GetEntity(Request.QueryString["ID"]);
        //        hidID.Value = obj.ID;
        //        hidBatchID.Value = obj.BatchID;
        //        ddlBatchNo.SelectedValue = obj.BatchID;
        //        ddlBatchNo.Enabled = false;
        //        numCounter.Text = obj.Counter.ToString();
        //        hidCounter.Value = obj.Counter.ToString();
        //        dtpPrintDate.Text = obj.PrintDate.ToString("yyyy-MM-dd");
        //        hidPrintDate.Value = obj.PrintDate.ToString("yyyy-MM-dd");
        //        dtpReportDate.Text = obj.ReportDate.ToString("yyyy-MM-dd");
        //        hidReportDate.Value = obj.ReportDate.ToString("yyyy-MM-dd");
        //        txtRemark1.Text = obj.Remark1;
        //        hidRemark1.Value = obj.Remark1;
        //        txtRemark2.Text = obj.Remark2;
        //        hidRemark2.Value = obj.Remark2;
        //        txtCreator.Text = obj.Creator;
        //        hidCreator.Value = obj.Creator;
        //        hidCreatorID.Value = obj.CreatorID;
        //        hidCreateTime.Value = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");


        //        DataTable dt = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetFWGSelectAT(ddlBatchNo.SelectedValue);
        //        dt.Columns.Add("Opinion", typeof(string));
        //        QueryParameters query = new QueryParameters();
        //        query.Add("FGWID",obj.ID, QueryParameters.OperatorType.Equal);
        //        DataTable dt2 = new BaseBusinessLogic<Bu_AuditTable_FGWDetail>().GetTable(query);
        //        DataRow[] rows = null;
        //        for (int i = dt.Rows.Count - 1; i >= 0; i--)
        //        {
        //            rows = dt2.Select("ATID='" + dt.Rows[0]["ID"].ToString() + "'");
        //            if (rows == null || rows.Length == 0)
        //            {
        //                dt.Rows.RemoveAt(i);
        //            }
        //            else
        //            {
        //                dt.Rows[i]["Opinion"] = rows[0]["Opinion"];
        //            }
        //        }
        //        rptList.DataSource = dt;
        //        rptList.DataBind();
        //    }
        //    else
        //    {
        //        hidID.Value = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
        //        hidCreatorID.Value = UserLoginer.LoginUser.ID;
        //        hidCreateTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        //        dtpPrintDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
        //        hidPrintDate.Value = DateTime.Today.ToString("yyyy-MM-dd");
        //    }
        //}

        private void Bind()
        {
            if (AuditSummaryUtils.ThisTimeFGWSession == null)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    AuditSummaryUtils.ThisTimeFGWSession = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetEditFGW(Request.QueryString["ID"]);
                    ddlBatchNo.Items.Insert(0, new ListItem(AuditSummaryUtils.ThisTimeFGWSession.BatchNo.ToString(), AuditSummaryUtils.ThisTimeFGWSession.BatchID));
                }
                else
                {
                    if (ddlBatchNo.SelectedValue != "0")//必须是有可选择的批次
                    {
                        AuditSummaryUtils.ThisTimeFGWSession = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetNewFGW(ddlBatchNo.SelectedValue);
                    }
                }
            }
            if (AuditSummaryUtils.ThisTimeFGWSession == null) { return; }
            ddlBatchNo.SelectedValue = AuditSummaryUtils.ThisTimeFGWSession.BatchID;
            numCounter.Text = AuditSummaryUtils.ThisTimeFGWSession.Counter.ToString();
            dtpPrintDate.Text = AuditSummaryUtils.ThisTimeFGWSession.PrintDate.ToString("yyyy-MM-dd");
            dtpReportDate.Text = AuditSummaryUtils.ThisTimeFGWSession.ReportDate.ToString("yyyy-MM-dd");
            txtRemark1.Text = AuditSummaryUtils.ThisTimeFGWSession.DisRemark1;
            txtRemark2.Text = AuditSummaryUtils.ThisTimeFGWSession.DisRemark2;
            txtCreator.Text = AuditSummaryUtils.ThisTimeFGWSession.Creator;
            rptList.DataSource = GetBindTable();
            rptList.DataBind();
        }

        private DataTable GetBindTable()
        {
            if (AuditSummaryUtils.ThisTimeFGWSession == null) { return null; }
           // Bu_AuditTable_FGW item = Session[AuditSummaryUtils.FWGSessionID] as Bu_AuditTable_FGW;
            DataTable dtBind = AuditSummaryUtils.ThisTimeFGWSession.BatchSummaryDetail.Copy();
            dtBind.Columns.Add("FGWOpinion", typeof(string));
            DataRow[] thisTimeRows = null;
            if (AuditSummaryUtils.ThisTimeFGWSession.ThisTimeAT != null && AuditSummaryUtils.ThisTimeFGWSession.ThisTimeAT.Rows.Count > 0)
            {
                for (int i = dtBind.Rows.Count - 1; i >= 0; i--)
                {
                    thisTimeRows = AuditSummaryUtils.ThisTimeFGWSession.ThisTimeAT.Select("ATID='" + dtBind.Rows[i]["ID"].ToString() + "'");
                    if (thisTimeRows.Length == 0)
                    {
                        dtBind.Rows.RemoveAt(i);
                    }
                    else
                    {
                        dtBind.Rows[i]["FGWOpinion"] = thisTimeRows[0]["Opinion"];
                    }
                }
            }
            else
            {
                dtBind.Rows.Clear();
            }
            return dtBind;
        }

        private void SetFGWOpinion()
        {
            if (hidFGWOpinion.Value != "")
            {
                string[] arrOp = hidFGWOpinion.Value.Split('|');
                hidFGWOpinion.Value = "";
                string[] arrIdOp = null;
                foreach (string s in arrOp)
                {
                    arrIdOp = s.Split('@');
                    foreach (DataRow dr in AuditSummaryUtils.ThisTimeFGWSession.ThisTimeAT.Rows)
                    {
                        if (dr["ATID"].ToString() == arrIdOp[0])
                        {
                            dr["Opinion"] = arrIdOp[1];
                            break;
                        }
                    }
                }
                //Session[AuditSummaryUtils.FWGSessionID] = item;
            }
        }
		
		protected void btnOK_Click(object sender, EventArgs e)
        {
            SetFGWOpinion();
            if (string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                AuditSummaryUtils.ThisTimeFGWSession.CreatorID = UserLoginer.LoginUser.ID;
                AuditSummaryUtils.ThisTimeFGWSession.CreateTime = DateTime.Now;
            }
            try
            {
                AuditSummaryUtils.ThisTimeFGWSession.PrintDate = Convert.ToDateTime(dtpPrintDate.Text);
                new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).SaveFGW(AuditSummaryUtils.ThisTimeFGWSession);
                Honestysoft.Baseframework.WebUI.ScriptHelper.Window.AlertClose("保存本次支付汇总表成功！", true);
                AuditSummaryUtils.ThisTimeFGWSession = null;//清空Session
            }
            catch (Exception ex)
            {
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                Honestysoft.Baseframework.WebUI.ScriptHelper.Window.AlertClose(ex.Message, false);
            }
        }

        protected string GetPDocRec(decimal amount_DocRec_S, decimal tAmount_C_A_S, decimal amount_C_S)
        {
            if (amount_C_S == 0) { return "0%"; }
            return string.Format("{0:P}", (amount_DocRec_S + tAmount_C_A_S) / amount_C_S);
        }

        protected string GetPS(decimal amount_S, decimal tAmount_C_A_S, decimal amount_C_S)
        {
            if (amount_C_S == 0) { return "0%"; }
            return string.Format("{0:P}", (amount_S + tAmount_C_A_S) / amount_C_S);
        }

        protected void ddlBatchNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            AuditSummaryUtils.ThisTimeFGWSession = null;//清空Session
            Bind();
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            TextBox txtOpinion = e.Item.FindControl("txtOpinion") as TextBox;
            txtOpinion.Attributes.Add("onchange", "setFGWOpinion('" + drv["ID"].ToString() + "','" + txtOpinion.ClientID + "');");
            txtOpinion.Text = drv["FGWOpinion"].ToString();
            txtOpinion.ToolTip = txtOpinion.Text;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            AuditSummaryUtils.ThisTimeFGWSession.PrintDate = Convert.ToDateTime(dtpPrintDate.Text);
            AuditSummaryUtils.ThisTimeFGWSession = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).DeleteFGWDetail(Request["chkID"].Split(','), AuditSummaryUtils.ThisTimeFGWSession);
            Bind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            AuditSummaryUtils.ThisTimeFGWSession.PrintDate = Convert.ToDateTime(dtpPrintDate.Text);
            if (hidAddATID.Value != "")
            {
                string[] arrID = hidAddATID.Value.Split(',');
                hidAddATID.Value = "";
                AuditSummaryUtils.ThisTimeFGWSession = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).AddFGWDetail(arrID, AuditSummaryUtils.ThisTimeFGWSession);
            }
            Bind();
        }
	}
}